package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bigkoo.pickerview.adapter.NumericWheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.lxj.xpopup.core.BottomPopupView;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class BottomShareTimeDialog  extends BottomPopupView {

    private BottomShareTimeDialog.DialogClickListener mDialogClickListener;
    private TextView tvLabel;
    private TextView tvSelectedTime;
    private WheelView wv_first;
    private WheelView wv_second;
    private WheelView wv_third;
    private WheelView wv_forth;
    private WheelView wv_fifth;
    private NumericWheelAdapter mFirstNumericWheelAdapter;
    private NumericWheelAdapter mSecondNumericWheelAdapter;
    private NumericWheelAdapter mThirdNumericWheelAdapter;
    private NumericWheelAdapter mFourthNumericWheelAdapter;
    private NumericWheelAdapter mFifthNumericWheelAdapter;
    private Calendar selected;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.sss");
    private SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy/MM/dd HH:mm");


    public BottomShareTimeDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_share_wheel_view;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        tvLabel = findViewById(R.id.tv_label);
        tvSelectedTime = findViewById(R.id.tv_selected_time);
        wv_first = findViewById(R.id.options1);
        wv_second = findViewById(R.id.options2);
        wv_third = findViewById(R.id.options3);
        wv_forth = findViewById(R.id.options4);
        wv_fifth = findViewById(R.id.options5);
        View btnSave = findViewById(R.id.btn_save);
        btnSave.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if(mDialogClickListener != null){
                    mDialogClickListener.onConfirmClick(selected.getTime());
                }
            }
        });
        tvLabel.setText("时间选择");
        setStyle();
        initWheelView();
    }

    private void setStyle() {
        wv_first.setGravity(Gravity.CENTER);
        wv_first.setCyclic(false);
        wv_first.setAlphaGradient(true);
//        wv_first.setLabelOffset(5);
        wv_first.setItemsVisibleCount(5);
        wv_first.setDividerColor(0x00000000);
        wv_first.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_first.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));

        wv_second.setGravity(Gravity.CENTER);
        wv_second.setCyclic(false);
        wv_second.setAlphaGradient(true);
//        wv_second.setLabelOffset(5);
        wv_second.setItemsVisibleCount(5);
        wv_second.setDividerColor(0x00000000);
        wv_second.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_second.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));

        wv_third.setGravity(Gravity.CENTER);
        wv_third.setCyclic(false);
        wv_third.setAlphaGradient(true);
//        wv_third.setLabelOffset(5);
        wv_third.setItemsVisibleCount(5);
        wv_third.setDividerColor(0x00000000);
        wv_third.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_third.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));

        wv_forth.setGravity(Gravity.CENTER);
        wv_forth.setCyclic(false);
        wv_forth.setAlphaGradient(true);
//        wv_third.setLabelOffset(5);
        wv_forth.setItemsVisibleCount(5);
        wv_forth.setDividerColor(0x00000000);
        wv_forth.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_forth.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));

        wv_fifth.setGravity(Gravity.CENTER);
        wv_fifth.setCyclic(false);
        wv_fifth.setAlphaGradient(true);
//        wv_third.setLabelOffset(5);
        wv_fifth.setItemsVisibleCount(5);
        wv_fifth.setDividerColor(0x00000000);
        wv_fifth.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_fifth.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));
    }

    private void initWheelView(){
        rebuildPicker();
        wv_first.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                int selectedYear = Integer.valueOf((String) mFirstNumericWheelAdapter.getItem(wv_first.getCurrentItem()));
                selected.set(Calendar.YEAR,selectedYear);
                rebuildPicker();
            }
        });
        wv_second.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                int selecteMonth = Integer.valueOf((String) mSecondNumericWheelAdapter.getItem(wv_second.getCurrentItem()));
                selected.set(Calendar.MONTH, selecteMonth - 1);
                rebuildPicker();
            }
        });
        wv_third.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                int selecteDay = Integer.valueOf((String) mThirdNumericWheelAdapter.getItem(wv_third.getCurrentItem()));
                selected.set(Calendar.DAY_OF_MONTH,selecteDay);
                rebuildPicker();
            }
        });
        tvSelectedTime.setText(formatText());

        wv_forth.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                int selecteHour = Integer.valueOf((String) mFourthNumericWheelAdapter.getItem(wv_forth.getCurrentItem()));
                selected.set(Calendar.HOUR_OF_DAY,selecteHour);
                rebuildPicker();
            }
        });

        wv_fifth.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                int selecteMin = Integer.valueOf((String) mFifthNumericWheelAdapter.getItem(wv_fifth.getCurrentItem()));
                selected.set(Calendar.MINUTE,selecteMin);
                rebuildPicker();
            }
        });
    }

    private void rebuildPicker(){
        Calendar currentCalendar = Calendar.getInstance();
        mFirstNumericWheelAdapter  = new NumericWheelAdapter(currentCalendar.get(Calendar.YEAR), currentCalendar.get(Calendar.YEAR) + 10);
        wv_first.setAdapter(mFirstNumericWheelAdapter);
        wv_first.setCurrentItem(selected.get(Calendar.YEAR)  - currentCalendar.get(Calendar.YEAR));

        int minMonth = 1;
        if(currentCalendar.get(Calendar.YEAR) == selected.get(Calendar.YEAR)){
            minMonth = currentCalendar.get(Calendar.MONTH) + 1;
        }
        mSecondNumericWheelAdapter  = new NumericWheelAdapter(minMonth, 12);
        wv_second.setAdapter(mSecondNumericWheelAdapter);
        wv_second.setCurrentItem(selected.get(Calendar.MONTH)+1 - minMonth );

        int minDay = 1;
        if(currentCalendar.get(Calendar.YEAR) == selected.get(Calendar.YEAR) && currentCalendar.get(Calendar.MONTH) == selected.get(Calendar.MONTH)){
            minDay = currentCalendar.get(Calendar.DAY_OF_MONTH);
        }
        mThirdNumericWheelAdapter  = new NumericWheelAdapter(minDay, selected.getActualMaximum(Calendar.DAY_OF_MONTH));
        wv_third.setAdapter(mThirdNumericWheelAdapter);
        if(selected.get(Calendar.DAY_OF_MONTH) < minDay){
            wv_third.setCurrentItem(0);
        }else{
            wv_third.setCurrentItem(selected.get(Calendar.DAY_OF_MONTH)-minDay);
        }

        int minHour = 0;
        if(currentCalendar.get(Calendar.YEAR) == selected.get(Calendar.YEAR) && currentCalendar.get(Calendar.MONTH) == selected.get(Calendar.MONTH) && currentCalendar.get(Calendar.DAY_OF_MONTH) == selected.get(Calendar.DAY_OF_MONTH)){
            minHour = currentCalendar.get(Calendar.HOUR_OF_DAY);
        }
        mFourthNumericWheelAdapter  = new NumericWheelAdapter(minHour, 23);
        wv_forth.setAdapter(mFourthNumericWheelAdapter);
        wv_forth.setCurrentItem(selected.get(Calendar.HOUR_OF_DAY)-minHour);

        int minMin = 0;
        if(currentCalendar.get(Calendar.YEAR) == selected.get(Calendar.YEAR) && currentCalendar.get(Calendar.MONTH) == selected.get(Calendar.MONTH) && currentCalendar.get(Calendar.DAY_OF_MONTH) == selected.get(Calendar.DAY_OF_MONTH)
                && currentCalendar.get(Calendar.HOUR_OF_DAY) == selected.get(Calendar.HOUR_OF_DAY)){
            minMin = currentCalendar.get(Calendar.MINUTE);
        }
        mFifthNumericWheelAdapter  = new NumericWheelAdapter(minMin, 59);
        wv_fifth.setAdapter(mFifthNumericWheelAdapter);
        wv_fifth.setCurrentItem(selected.get(Calendar.MINUTE)-minMin);

        tvSelectedTime.setText(formatText());
    }

    private String formatText(){

        return simpleDateFormat1.format(selected.getTime());
    }

    public void setSelectDate(String date){
        if(TextUtils.isEmpty(date)){
            return;
        }
        try {
            Date current = simpleDateFormat.parse(date);
            selected = Calendar.getInstance();
            selected.setTime(current);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if(wv_fifth != null){
            rebuildPicker();
        }
    }

    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }

    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    protected int getPopupHeight() {
        return 0;
    }

    public void setDialogClickListener(@Nullable BottomShareTimeDialog.DialogClickListener listener) {
        mDialogClickListener = listener;
    }

    public interface DialogClickListener {
        void onConfirmClick(Date date);
    }
}